package com.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 羡羡
 */
@WebFilter("/*")
public class IndexFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        String url=request.getRequestURI();
        String name= (String) request.getSession().getAttribute("uname");
        String controlurl ="/LeaveMessage/index.jsp";
        if(name==null && url.equals(controlurl)){
            response.sendRedirect("/LeaveMessage/login.jsp");
        }
        chain.doFilter(req, resp);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
